import Base from 'models/base';

export default class Location extends Base {
  constructor(data, isDefault=false) {
    super();

    let locationRes = data.locationName;
    if (locationRes.status === 0) {
      this.locationMsg = locationRes.message;

      let locationResult = locationRes.result;
      let locationTown = locationResult.address_reference.town;
      let addressDetail = locationResult.ad_info;

      let locationText;
      if (isDefault) {
        locationText = addressDetail.district;
      } else {
        locationText = locationTown.title;
      }

      let locationData = {
        address: locationResult.address,
        addressDetail: addressDetail,
        locationText: locationText
      }
      this.locationData = locationData;
      this.lnglat = locationResult.location;
    } else {
      this.locationData = null;
      this.locationMsg = locationRes.message;
      this.lnglat = null;
    }

    let briefconditionRes = data.briefcondition;
    if (briefconditionRes === null) {
      this.weatherData = null;
      this.weatherMsg = '';
    } else if (briefconditionRes.code === 0) {
      let locationResult = briefconditionRes.data;
      this.weatherData = locationResult.condition;
      this.weatherMsg = briefconditionRes.msg;
    } else {
      this.weatherData = null;
      this.weatherMsg = briefconditionRes.msg;
    }
  }
}
